const BaseIpcHandler = require('../BaseIpcHandler');
const path = require('path');
const { DatasetUtils } = require('../../utils/datasetUtils');

class DownloadDatasetHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> DownloadDatasetHandler params = ', params);
        let { dataset_info } = params;
        dataset_info = JSON.parse(dataset_info);

        // 业务逻辑
        let result = null;
        try {
            const dataset_utils = new DatasetUtils(this.app);
            const unzip_dir = path.join(this.app.userDataPath, 'downloads', dataset_info.id);
            result = await dataset_utils.downloadAndInstall(dataset_info);
        } catch (err) {
            console.error('Error get dataset cloud list', err);
            result = { code: -1, msg: err.message };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = DownloadDatasetHandler;
